<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a>客房中心</a></el-breadcrumb-item>
      <el-breadcrumb-item>添加房间</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-col :span="12" style="padding-right: 20px">
    </el-col>
    <el-card class="box-one" >
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="房间编号" prop="roomId">
          <el-input type="text" v-model="ruleForm.roomId"></el-input>
          </el-form-item>
            <el-form-item label="房间名称" prop="roomName">
          <el-input type="text" v-model="ruleForm.roomName"></el-input>
        </el-form-item>
        <el-form-item label="房间简介" >
          <el-input type="text" v-model="ruleForm.description" class="input"></el-input>
        </el-form-item>
        <el-form-item label="房间价格">
          <el-input type="text" v-model="ruleForm.price" class="input"></el-input>
        </el-form-item>
        <el-form-item label="房间原价">
          <el-input type="text" v-model="ruleForm.oldPrice" class="input"></el-input>
        </el-form-item>
            <el-form-item label="房间类型">
              <el-select placeholder="请选择" v-model="ruleForm.type">
                <el-option v-for="item in roomType"
                :key="item.id"
                lable="item.type"
                :value="item.type"></el-option>
              </el-select>
            </el-form-item>
        <el-form-item label="房间图片">
          <img :src="ruleForm.url" width="80px" alt="">
          <el-upload
              :headers="{Authorization:this.jwt}"
              action='http://localhost:9085/rooms/upload'
              name="picFile"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              :on-remove="handleRemove"
              :on-success="success">

            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
      <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  data: function () {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      roomType:[{type:'单人房'} ,{type:'双人房'} ,{type:'总统套房'} ,{type:'电竞房'} ,{type:'大床房'},],
      ruleForm: {type: "", price: "", oldPrice: "",  num: "", roomId: "", url: "", roomName: "",description:""},
      newUrl: "",
      jwt:'',
      rules: {
        roomName: [
          {required: true, message: '请输入房间名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2到 15 个字符', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请输入房间类型', trigger: 'blur'},
          {min: 2, max: 35, message: '长度在 4 到 35 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入房间类型', trigger: 'blur'},
          {min: 2, max: 35, message: '长度在 4 到 35 个字符', trigger: 'blur'}
        ],
        price: [
          {required: true, message: '请输入房间价格', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '价格必须是数字', trigger: 'blur'}
        ],
        oldPrice: [
          {required: true, message: '请输入历史价格', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '价格必须是数字', trigger: 'blur'}
        ],
        roomId: [
          {required: true, message: '请输入房间Id', trigger: 'blur'},
          {pattern: /^(\d{1}|[0-9][0-9]{1}[0-9]?)$/, message: '房间Id必须是1-999的数字', trigger: 'blur'}
        ],
      }
    }
  },

  methods: {
    success(response,file,fileList){
    this.ruleForm.url=response;
      console.log(file)
      console.log(this.ruleForm.url)
      console.log(fileList)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },


    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9085/rooms/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log( formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加房间成功！',
                type: 'success'
              });
              this.resetForm(formName);
              this.$router.push({path:'/rs2'});
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.jwt=localStorage.getItem('jwt')
  }
}

</script>

<style scoped>

.box-one {

  margin-bottom: 200px;
}

.block {
  text-align: center;
  margin-top: 100px;
}
.input{
  width: 500px;
}
</style>
